package com.ruitu.mall_98_2.mvp.presenter;

import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.mvp.contract.CommitOrderContract;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.mine.Coupon;
import com.ruitu.router_module.bean.order.CartSettlement;
import com.ruitu.router_module.bean.order.ConfirOrder;

import java.util.List;

public class CommitOrderPresenter extends CommitOrderContract.Presenter {
    @Override
    public void cartSettlement(String accountId, String ids, String addressId) {
        mModel.cartSettlement(accountId, ids, addressId)
                .subscribe(new SubscriberImpl<BaseResult<CartSettlement>>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        mView.setFails();
                        ToastUtils.showShort("获取结算信息失败");
                        Logg.i(e.toString());
                    }

                    @Override
                    public void onNext(BaseResult<CartSettlement> baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.setSuc(baseResult.getData());
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void selectUseCoupons(String accountId, String userCouponIds, String goodId, String totalMoney, String companyId) {
        mModel.selectUseCoupons(accountId, userCouponIds, goodId, totalMoney, companyId)
                .subscribe(new SubscriberImpl<BaseResult<List<Coupon>>>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        ToastUtils.showShort("获取优惠券失败");
                    }

                    @Override
                    public void onNext(BaseResult<List<Coupon>> listBaseResult) {
                        mView.hideProgress();
                        if (listBaseResult.isSuccess()) {
                            mView.setCoupon(listBaseResult.getData());
                        } else {
                            ToastUtils.showShort(listBaseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void cartForOrder(String accountId, String ids, String addressId) {
        mModel.cartForOrder(accountId, ids, addressId)
                .subscribe(new SubscriberImpl<BaseResult<ConfirOrder>>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                    }

                    @Override
                    public void onNext(BaseResult<ConfirOrder> baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.setForOrder(baseResult.getData());
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }
}
